package com.heyige.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heyige.common.CommResponse;
import com.heyige.dto.PromptTemplateDto;
import com.heyige.entity.PromptTemplate;
import com.heyige.service.PromptTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("api/promptTemplates")
@RequiredArgsConstructor
@Tag(name = "提示词模板", description = "提示词模板管理相关接口")
public class PromptTemplateController {

    private final PromptTemplateService promptTemplateService;

    /**
     * 获取提示词模板列表
     */
    @GetMapping
    @Operation(summary = "获取提示词模板列表", description = "分页获取提示词模板列表")
    public CommResponse<IPage<PromptTemplateDto.TemplateResponse>> getPromptTemplates(
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<PromptTemplateDto.TemplateResponse> templates = promptTemplateService.getPromptTemplates(new Page<>(page, size));
        return CommResponse.ok(templates);
    }

    /**
     * 根据类型获取提示词模板列表
     */
    @GetMapping("type/{type}")
    @Operation(summary = "根据类型获取提示词模板", description = "根据模板类型分页获取提示词模板列表")
    public CommResponse<IPage<PromptTemplateDto.TemplateResponse>> getPromptTemplatesByType(
            @Parameter(description = "模板类型", required = true, example = "GENERAL") @PathVariable PromptTemplate.TemplateType type,
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<PromptTemplateDto.TemplateResponse> templates = promptTemplateService.getPromptTemplatesByType(type, new Page<>(page, size));
        return CommResponse.ok(templates);
    }

    /**
     * 根据分类获取提示词模板列表
     */
    @GetMapping("category/{category}")
    @Operation(summary = "根据分类获取提示词模板", description = "根据模板分类分页获取提示词模板列表")
    public CommResponse<IPage<PromptTemplateDto.TemplateResponse>> getPromptTemplatesByCategory(
            @Parameter(description = "模板分类", required = true, example = "写作") @PathVariable String category,
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<PromptTemplateDto.TemplateResponse> templates = promptTemplateService.getPromptTemplatesByCategory(category, new Page<>(page, size));
        return CommResponse.ok(templates);
    }

    /**
     * 根据标签搜索提示词模板列表
     */
    @GetMapping("tags")
    @Operation(summary = "根据标签搜索提示词模板", description = "根据标签分页搜索提示词模板列表")
    public CommResponse<IPage<PromptTemplateDto.TemplateResponse>> getPromptTemplatesByTags(
            @Parameter(description = "标签", required = true, example = "AI,写作") @RequestParam String tags,
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<PromptTemplateDto.TemplateResponse> templates = promptTemplateService.getPromptTemplatesByTags(tags, new Page<>(page, size));
        return CommResponse.ok(templates);
    }

    /**
     * 获取用户的提示词模板列表
     */
    @GetMapping("my")
    @Operation(summary = "获取用户的提示词模板", description = "分页获取用户创建的提示词模板列表")
    public CommResponse<IPage<PromptTemplateDto.TemplateResponse>> getUserPromptTemplates(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<PromptTemplateDto.TemplateResponse> templates = promptTemplateService.getUserPromptTemplates(userId, new Page<>(page, size));
        return CommResponse.ok(templates);
    }

    /**
     * 获取公共提示词模板列表
     */
    @GetMapping("public")
    @Operation(summary = "获取公共提示词模板", description = "分页获取公共提示词模板列表")
    public CommResponse<IPage<PromptTemplateDto.TemplateResponse>> getPublicPromptTemplates(
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<PromptTemplateDto.TemplateResponse> templates = promptTemplateService.getPublicPromptTemplates(new Page<>(page, size));
        return CommResponse.ok(templates);
    }

    /**
     * 获取提示词模板详情
     */
    @GetMapping("{id}")
    @Operation(summary = "获取提示词模板详情", description = "根据ID获取提示词模板详情")
    public CommResponse<PromptTemplateDto.TemplateResponse> getPromptTemplate(
            @Parameter(description = "模板ID", required = true, example = "1") @PathVariable Long id) {
        PromptTemplateDto.TemplateResponse template = promptTemplateService.getPromptTemplate(id);
        return CommResponse.ok(template);
    }

    /**
     * 添加提示词模板
     */
    @PostMapping
    @Operation(summary = "添加提示词模板", description = "创建新的提示词模板")
    public CommResponse<PromptTemplateDto.TemplateResponse> addPromptTemplate(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "添加模板请求参数") @Valid @RequestBody PromptTemplateDto.AddRequest request) {
        PromptTemplateDto.TemplateResponse template = promptTemplateService.addPromptTemplate(userId, request);
        return CommResponse.ok(template);
    }

    /**
     * 更新提示词模板
     */
    @PutMapping("{id}")
    @Operation(summary = "更新提示词模板", description = "更新指定的提示词模板")
    public CommResponse<PromptTemplateDto.TemplateResponse> updatePromptTemplate(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "模板ID", required = true, example = "1") @PathVariable Long id,
            @Parameter(description = "更新模板请求参数") @Valid @RequestBody PromptTemplateDto.UpdateRequest request) {
        PromptTemplateDto.TemplateResponse template = promptTemplateService.updatePromptTemplate(id, userId, request);
        return CommResponse.ok(template);
    }

    /**
     * 删除提示词模板
     */
    @DeleteMapping("{id}")
    @Operation(summary = "删除提示词模板", description = "删除指定的提示词模板")
    public CommResponse<Void> deletePromptTemplate(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "模板ID", required = true, example = "1") @PathVariable Long id) {
        promptTemplateService.deletePromptTemplate(id, userId);
        return CommResponse.ok();
    }

    /**
     * 复制提示词模板
     */
    @PostMapping("{id}/copy")
    @Operation(summary = "复制提示词模板", description = "复制指定的提示词模板")
    public CommResponse<PromptTemplateDto.TemplateResponse> copyPromptTemplate(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "模板ID", required = true, example = "1") @PathVariable Long id) {
        PromptTemplateDto.TemplateResponse template = promptTemplateService.copyPromptTemplate(id, userId);
        return CommResponse.ok(template);
    }

    /**
     * 处理提示词模板
     */
    @PostMapping("{id}/process")
    @Operation(summary = "处理提示词模板", description = "使用变量处理提示词模板")
    public CommResponse<PromptTemplateDto.ProcessResult> processPromptTemplate(
            @Parameter(description = "模板ID", required = true, example = "1") @PathVariable Long id,
            @Parameter(description = "变量映射") @RequestBody Map<String, String> variables) {
        PromptTemplateDto.ProcessResult result = promptTemplateService.processPromptTemplate(id, variables);
        return CommResponse.ok(result);
    }

    /**
     * 获取热门提示词模板
     */
    @GetMapping("hot")
    @Operation(summary = "获取热门提示词模板", description = "获取热门的提示词模板列表")
    public CommResponse<List<PromptTemplateDto.TemplateResponse>> getHotPromptTemplates(
            @Parameter(description = "限制数量", example = "10") @RequestParam(defaultValue = "10") Integer limit) {
        List<PromptTemplateDto.TemplateResponse> templates = promptTemplateService.getHotPromptTemplates(limit);
        return CommResponse.ok(templates);
    }

    /**
     * 获取小红书文案模板
     */
    @GetMapping("redBook")
    @Operation(summary = "获取小红书文案模板", description = "分页获取小红书文案模板列表")
    public CommResponse<IPage<PromptTemplateDto.TemplateResponse>> getRedBookTemplates(
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<PromptTemplateDto.TemplateResponse> templates = promptTemplateService.getRedBookTemplates(new Page<>(page, size));
        return CommResponse.ok(templates);
    }
}